<?php
namespace Home\Controller;
use Think\Log;

class GitController extends HomeController {
    function pull()
    {
        $target='/var/www/wxgame';
        $token='lazycao';
        $wwwUser='www';
        $wwwGroup='www';
        $json=json_decode(file_get_contents('php://input'),true);
        Log::write('git push钩子'.file_get_contents('php://input'));
        if(empty($json['password'])||$json['password']!==$token) {
            exit('errorrequest');
        }

        $repo=$json['repository']['name'];



        $cmds = array(
            "whoami",
            "cd1 $target",
            "git checkout master",
            "git pull",
//            "chown -R {$wwwUser}:{$wwwGroup} $target/",
        );

//
        $res = 0;
        foreach ($cmds as $cmd) {
            exec($cmd,$res);
            Log::write('git push钩子执行结果'.json_encode($res));
        }
exit('成功');
//        $cmd="sudo -Hu root cd $target && git pull";
//        $res = shell_exec($cmd);

    }
}